package com.tensquare.user.controller;

import com.tensquare.user.pojo.Admin;
import com.tensquare.user.service.AdminService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import util.JwtUtil;

/**
 * 控制器层.
 *
 * @author Administrator
 */
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {

  @Autowired private AdminService adminService;
  @Resource private JwtUtil jwtUtil;

  /**
   * 登录.
   *
   * @param admin 登录信息.
   * @return 结果.
   */
  @PostMapping("/login")
  public Result login(@RequestBody Admin admin) {
    admin = adminService.login(admin);
    if (admin == null) {
      return new Result(true, StatusCode.LOGINERROR, "登录失败");
    }

    String token = jwtUtil.createJWT(admin.getId(), admin.getLoginname(), "admin");
    /** 将权限和token返回给前端. */
    Map<String, Object> map = new HashMap<>();
    map.put("role", "admin");
    map.put("token", token);

    // 生成令牌
    return new Result(true, StatusCode.OK, "登录成功", map);
  }

  /**
   * 注册.
   *
   * @param admin
   */
  @RequestMapping(method = RequestMethod.POST)
  public Result add(@RequestBody Admin admin) {
    adminService.add(admin);
    return new Result(true, StatusCode.OK, "增加成功");
  }

  /**
   * 查询全部数据.
   *
   * @return
   */
  @RequestMapping(method = RequestMethod.GET)
  public Result findAll() {
    return new Result(true, StatusCode.OK, "查询成功", adminService.findAll());
  }

  /**
   * 根据ID查询.
   *
   * @param id ID
   * @return
   */
  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  public Result findById(@PathVariable String id) {
    return new Result(true, StatusCode.OK, "查询成功", adminService.findById(id));
  }

  /**
   * 分页+多条件查询.
   *
   * @param searchMap 查询条件封装
   * @param page 页码
   * @param size 页大小
   * @return 分页结果
   */
  @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
  public Result findSearch(
      @RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
    Page<Admin> pageList = adminService.findSearch(searchMap, page, size);
    return new Result(
        true,
        StatusCode.OK,
        "查询成功",
        new PageResult<Admin>(pageList.getTotalElements(), pageList.getContent()));
  }

  /**
   * 根据条件查询.
   *
   * @param searchMap
   * @return
   */
  @RequestMapping(value = "/search", method = RequestMethod.POST)
  public Result findSearch(@RequestBody Map searchMap) {
    return new Result(true, StatusCode.OK, "查询成功", adminService.findSearch(searchMap));
  }

  /**
   * 修改.
   *
   * @param admin
   */
  @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
  public Result update(@RequestBody Admin admin, @PathVariable String id) {
    admin.setId(id);
    adminService.update(admin);
    return new Result(true, StatusCode.OK, "修改成功");
  }

  /**
   * 删除.
   *
   * @param id
   */
  @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  public Result delete(@PathVariable String id) {
    adminService.deleteById(id);
    return new Result(true, StatusCode.OK, "删除成功");
  }
}
